﻿using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Modules.Corp;
using Modules.Project;
using ManagementApi.Models;
using ManagementApi.Models.Corp;
using ManagementApi.Mvc;
using PmSoft.Entity;
using System.Security.Authentication;
using Microsoft.AspNetCore.Authorization;
using ManagementApi.Mvc.ApplicationContexts;

namespace ManagementApi.Controllers
{
    /// <summary>
    /// 已登录用户通用业务控制器
    /// </summary>
    [Route("/api/v1/user_common")]
    [ApiController]
    [Authorize]
    public class UserCommonController : ControllerBase
    {
        /// <summary>
        /// 请求上下文
        /// </summary>
        private readonly ManagementApplicationContext applicationContext;
        /// <summary>
        /// 当前登录用户(可转换为类型：SysUser、CorpInfo、ConstructorInfo、ManagerInfo、CommanderInfo)
        /// </summary>
        private IUser CurrentUser => applicationContext.CurrentUser ?? throw new AuthenticationException();

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="applicationContext"></param>
        public UserCommonController(ManagementApplicationContext applicationContext)
        {
            this.applicationContext = applicationContext;
        }

        /// <summary>
        /// 搜索企业(企业完整信息)
        /// </summary>
        /// <param name="keywords"></param>
        /// <returns></returns>
        [HttpGet("search_fullcorps")]
        public ApiResult<IEnumerable<CorpInfoDto>> SearchCorps([Required][MinLength(2)] string keywords)
        {
            var corps = applicationContext.GetService<CorpService>().SearchCorps(keywords, 10);
            var dtos = corps.Select(m => m.AsDto(applicationContext));
            return ApiResult<IEnumerable<CorpInfoDto>>.Success(dtos);
        }
    }
}
